﻿Imports System.Data.OleDb

Public Class addnewmovie
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Function getSuffix(ByVal fn As String)
        Dim fileParts = fn.Split(".")
        Dim suffix = fileParts(fileParts.Length - 1)
        Return "." & suffix
    End Function

    Protected Sub add_Click(sender As Object, e As EventArgs) Handles add.Click
        Dim newFileName As String
        newFileName = Guid.NewGuid().ToString() & getSuffix(fup_RPicture.FileName)
        If fup_RPicture.HasFile Then
            fup_RPicture.SaveAs(Server.MapPath("Images") & "/" & newFileName)
        End If


        Dim sql As String = "INSERT INTO movies (m_title, m_desc, m_starring, m_pg, m_poster, m_year, sub_id, m_date, m_duration, m_addby  ) " & _
            " VALUES (@f1,@f2,@f3,@f4,@f5,@f6,@f7,Now(),@f9,@f10)"
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@f1", TB_m_title.Text)
        cmd.Parameters.AddWithValue("@f2", TB_m_desc.Text)
        cmd.Parameters.AddWithValue("@f3", TB_m_starring.Text)
        cmd.Parameters.AddWithValue("@f4", TB_m_pg.Text)
        cmd.Parameters.AddWithValue("@f5", newFileName)
        cmd.Parameters.AddWithValue("@f6", TB_m_year.Text)
        cmd.Parameters.AddWithValue("@f7", DDL_sub_id.SelectedValue)
        cmd.Parameters.AddWithValue("@f9", TB_m_duration.Text)
        cmd.Parameters.AddWithValue("@f10", User.Identity.Name)

        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()

        Reputation()
        Response.Redirect("list.aspx")
    End Sub


    Sub Reputation()


        Dim SqlString As String
        Dim Countposts As Long
        Dim temp As String

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)


        SqlString = "SELECT Countpost, m_addby FROM Countposts where m_addby = @1 "
        Dim cmd1 As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd1.CommandType = CommandType.Text
        oleDbConn.Open()
        cmd1.Parameters.AddWithValue("@f1", User.Identity.Name)
        Dim dr As OleDbDataReader = cmd1.ExecuteReader()
        If dr.HasRows Then
            dr.Read()
            If Not IsDBNull(dr("Countpost")) Then Countposts = dr("Countpost") Else Countposts = 0
        End If


        If Countposts < 50 Then
            temp = "Newbie"
        ElseIf Countposts >= 50 And Countposts < 100 Then
            temp = "Intermediate"
        ElseIf Countposts >= 100 And Countposts < 200 Then
            temp = "Expert"
        ElseIf Countposts >= 200 And Countposts < 400 Then
            temp = "Master"
        ElseIf Countposts >= 400 Then
            temp = "Artisan"
        End If

        SqlString = "Update Profile set Reputation=@f1 where Username = @f2"
        Dim cmd2 As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        cmd2.Parameters.AddWithValue("@f1", temp)
        cmd2.Parameters.AddWithValue("@f2", User.Identity.Name)

        cmd2.ExecuteNonQuery()
        oleDbConn.Close()
        cmd2.Dispose()
        oleDbConn.Dispose()

    End Sub
End Class